package com.ayh.campusfun.controller.api.v1;

import cn.hutool.core.bean.BeanUtil;
import com.ayh.campusfun.common.CommonResult;
import com.ayh.campusfun.common.PageResult;
import com.ayh.campusfun.common.core.jwt.JWTUtils;
import com.ayh.campusfun.common.exception.http.ForbiddenException;
import com.ayh.campusfun.entity.*;
import com.ayh.campusfun.service.*;
import com.ayh.campusfun.vo.ShareVO;
import com.ayh.campusfun.vo.TopicVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ayh
 * @create 2021/4/1 14:04
 */

@RestController
@RequestMapping("dynamic")
public class DynamicController {

    @Autowired
    private TopicShareService topicShareService;

    @Autowired
    private FunShareService funShareService;

    @Autowired
    private LikeFunTopicService likeFunTopicService;

    @Autowired
    private AttentionService attentionService;

    @Autowired
    private GoodFriendService goodFriendService;

    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    @Resource
    private JWTUtils jwtUtils;

    @PostMapping(value = "/findDynamicList",produces = "application/json")
    public CommonResult getDynamicList(
            HttpServletRequest request,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "1") Integer type,
            @RequestParam(defaultValue = "1") Integer attentionOrDynamic
    ){
        String authorization = jwtUtils.getTokenFromRequest(request);
        Users users = (Users) redisTemplate.opsForValue().get(authorization);
        String uid = users.getUid();
        if(users==null){
            throw new ForbiddenException(60009);
        }

        Map<String,Object> map = new HashMap<>();
        map.put("uid",users.getUid());
        List<String> friendIds = new ArrayList<>();
        if(attentionOrDynamic==1) {
            friendIds = goodFriendService.findListByParam(map).stream()
                    .map(v -> v.getFriendId()).collect(Collectors.toList());
        }else if(attentionOrDynamic==2){
            friendIds = attentionService.findListByParam(map).stream()
                    .map(v -> v.getAttentionId()).collect(Collectors.toList());
        }
        map.clear();
        map.put("limit",limit);
        map.put("page",page);
        map.put("friendIds",friendIds);
        map.put("order","send_time desc");

        Map<String,Object> queryMap = new HashMap<>();
        if(type==1) {
            PageResult topicSharePageResult = topicShareService.findAll(map);
            List<TopicShare> topicShareList = topicSharePageResult.getData();

            topicSharePageResult.setData(topicShareList.stream()
                    .map(v -> {
                        TopicVO topicVO = new TopicVO();
                        BeanUtil.copyProperties(v, topicVO);
                        boolean isClick = false;
                        boolean isAttention = false;
                        queryMap.put("uid", uid);
                        queryMap.put("funId", v.getId());
                        queryMap.put("funType", 2);
                        LikeFunTopic likeFunTopic = likeFunTopicService.findByParam(queryMap);
                        queryMap.clear();
                        queryMap.put("uid", uid);
                        queryMap.put("attentionId", v.getUid());
                        Attention attention = attentionService.findByParam(queryMap);
                        if (likeFunTopic != null) {
                            isClick = true;
                        }
                        if (attention != null) {
                            isAttention = true;
                        }
                        topicVO.setIsClick(isClick);
                        topicVO.setIsAttention(isAttention);
                        return topicVO;
                    }).collect(Collectors.toList()));
            return CommonResult.success(topicShareList);
        }else if(type==2) {
            PageResult funSharePageResult = funShareService.findAll(map);
            List<FunShare> funShareList = funSharePageResult.getData();

            funSharePageResult.setData(funShareList.stream()
                    .map(v -> {
                        ShareVO shareVO = new ShareVO();
                        BeanUtil.copyProperties(v, shareVO);
                        boolean isClick = false;
                        boolean isAttention = false;
                        queryMap.put("uid", uid);
                        queryMap.put("funId", v.getId());
                        queryMap.put("funType", 1);
                        LikeFunTopic likeFunTopic = likeFunTopicService.findByParam(queryMap);
                        queryMap.clear();
                        queryMap.put("uid", uid);
                        queryMap.put("attentionId", v.getUid());
                        Attention attention = attentionService.findByParam(queryMap);
                        if (likeFunTopic != null) {
                            isClick = true;
                        }
                        if (attention != null) {
                            isAttention = true;
                        }
                        shareVO.setIsAttention(isAttention);
                        shareVO.setIsClick(isClick);
                        return shareVO;
                    }).collect(Collectors.toList()));
            return CommonResult.success(funSharePageResult);
        }else {
            return null;
        }
    }
}
